//
//  HomeViewController.swift
//  SpinCut
//
//  Created by dai simon on 2024/7/6.
//

import UIKit
import SnapKit

struct DesignItem {
    var title: String = ""
}

class HomeViewController : BaseViewController {
    lazy var topBar: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#000000")
        return view
    }()
    
    lazy var logoImageView : UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "icon_logo")
        return view
    }()
    
    lazy var appNameLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.boldSystemFont(ofSize: 30)
        view.textColor = .white
        view.text = "SpinArt"
        return view
    }()
    
    lazy var collectionView : UICollectionView = {
        let y = ScreenStatusBarHeight
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: y, width: ScreenWidth, height: ScreenHeight - ScreenTabBarSafeBottomMargin - y), collectionViewLayout: UICollectionViewFlowLayout())
        collectionView.tag = 1000
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = UIColor(hex: "#222222")
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.register(DesignCell.self, forCellWithReuseIdentifier: "DesignCell")
        return collectionView
    }()
    
    var listData: [DesignItem] = []
    let cellWidth: CGFloat = (ScreenWidth - 36 * 4) / 5.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        self.navigationController?.navigationBar.isTranslucent = true
        
        self.view.addSubview(self.topBar)
        self.topBar.snp.makeConstraints { make in
            make.height.equalTo(51 + ScreenStatusBarHeight)
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
        }
        
        topBar.addSubview(self.logoImageView)
        self.logoImageView.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.left.equalToSuperview().offset(18)
            make.bottom.equalToSuperview().offset(-8)
        }
        
        topBar.addSubview(self.appNameLabel)
        self.appNameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(logoImageView)
            make.left.equalTo(logoImageView.snp.right).offset(4)
        }
        
        self.view.addSubview(collectionView)
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(self.topBar.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        self.view.backgroundColor = UIColor.black
        
        setupData()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    
    func setupData() {
        listData = [
            DesignItem(title: "Telegram"),
            DesignItem(title: "Viber"),
            DesignItem(title: "Facebook"),
            DesignItem(title: "Instagram"),
            DesignItem(title: "Tiktok"),
            DesignItem(title: "Spotify")
        ]
        
    }
}

extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return listData.count
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let data = listData[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DesignCell", for: indexPath) as! DesignCell
        cell.setup(data.title)
        return cell
    }
 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        let data = listData[indexPath.row]
        let controller = EditViewController()
        self.navigationController?.pushViewController(controller, animated: true)
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellHeight: CGFloat = cellWidth + 11
        return CGSize(width: cellWidth, height: cellHeight)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 25
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 36
    }
}

